package com.adt.ferreprob.dao.imp;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.transaction.annotation.Transactional;

import com.adt.ferreprob.dao.IFacturaDao;
import com.adt.ferreprob.modelo.Factura;
import com.adt.ferreprob.modelo.Proveedor;

@Transactional
public class FacturaDao implements IFacturaDao {
	
	private static Log log = LogFactory.getLog(FacturaDao.class);
	private String className = FacturaDao.class.getName();
	private EntityManager entityManager;
	
	@PersistenceContext
	public void setEntityManager(EntityManager entityManager) {
		this.entityManager = entityManager;
	}
	
	@Override
	public <T> boolean agregar(T objDominio) {
		log.info(className + ".agregar()");
		
		try {
			
			entityManager.persist(objDominio);
			
		} catch (Exception e) {
			log.error("Error en funcion: " + className + ".agregar() - Mensaje de Error:" +  e.getMessage());
			return false;
		}
		return true;
	}

	@Override
	public <T> boolean editar(T objDominio) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public <T> boolean borrar(T objDominio) {
		log.info(className + ".borrar()");
		
		try {
			Factura factura = entityManager.find(Factura.class, ((Factura)objDominio).getIdFactura());
			entityManager.remove(factura);
			
		} catch (Exception e) {
			log.error("Error en funcion: " + className + ".borrar() - Mensaje de Error:" +  e.getMessage());
			return false;
		}
		
		return true;
	}

	@Override
	public <T> T obtener(Integer id) {
		// TODO Auto-generated method stub
		return null;
	}

}
